package com.guangwei.arithmetictest;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    EditText etTotalQuality, etNumberRange;
    TextView tvVersion;
    CheckBox cbAdd, cbSub, cbMul, cbDiv;

    private Integer mTotalQuality = 50;
    private Integer mMaxValue = 100;
    private Boolean mIsAddEnable = true;
    private Boolean mIsSubEnable = true;
    private Boolean mIsMulEnable = false;
    private Boolean mIsDivEnable = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        getSharedPref();
        findView();
        initView();
        Button btnStartTest = findViewById(R.id.btn_start_test);
        Button btnCheckScore = findViewById(R.id.btn_check_history_score);
        btnStartTest.setOnClickListener(this);
        btnCheckScore.setOnClickListener(this);
        SystemTTS.getInstance(this).speak(""); // workaround for "TextToSpeech: speak failed: not bound to TTS engine"
    }

    private  void findView() {
        etTotalQuality = findViewById(R.id.et_test_quality);
        etNumberRange = findViewById(R.id.et_number_range);
        cbAdd = findViewById(R.id.cb_add);
        cbSub = findViewById(R.id.cb_sub);
        cbMul = findViewById(R.id.cb_mul);
        cbDiv = findViewById(R.id.cb_div);
        tvVersion = findViewById(R.id.tv_version);

    }

    private void initView() {
        etTotalQuality.setText(Integer.toString(mTotalQuality));
        etNumberRange.setText(Integer.toString(mMaxValue));
        cbAdd.setChecked(mIsAddEnable);
        cbSub.setChecked(mIsSubEnable);
        cbMul.setChecked(mIsMulEnable);
        cbDiv.setChecked(mIsDivEnable);
        tvVersion.setText(getString(R.string.ver_info)+BuildConfig.VERSION_NAME);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_start_test:
                mTotalQuality = Integer.valueOf(etTotalQuality.getText().toString());
                mMaxValue = Integer.valueOf(etNumberRange.getText().toString());
                mIsAddEnable = cbAdd.isChecked();
                mIsSubEnable = cbSub.isChecked();
                mIsMulEnable = cbMul.isChecked();
                mIsDivEnable = cbDiv.isChecked();

                boolean inputValide = true;
                if (mTotalQuality<1) {
                    showWarningDialog("总题目数小于1，请重新输入总题目数！");
                    break;
                }
                if (mMaxValue<2) {
                    showWarningDialog("数值范围小于2，请重新输入数值范围！");
                    break;
                }
                if (!(mIsAddEnable || mIsSubEnable || mIsMulEnable || mIsDivEnable)) {
                    showWarningDialog("测试类型至少选择一个，请重新选择测试类型！");
                    break;
                }

                setSharedPref();

                Intent intent = new Intent();
                intent.setAction("test_ui_action");
                intent.addCategory("test_ui_category");
                Bundle bd = new Bundle();
                bd.putInt("TotalQuality", mTotalQuality);
                bd.putInt("MaxValue", mMaxValue);
                bd.putBoolean("IsAddEnable", mIsAddEnable);
                bd.putBoolean("IsSubEnable", mIsSubEnable);
                bd.putBoolean("IsMulEnable", mIsMulEnable);
                bd.putBoolean("IsDivEnable", mIsDivEnable);
                intent.putExtras(bd);
                startActivity(intent);
                break;
            case R.id.btn_check_history_score:
                intent = new Intent();
                intent.setAction("time_line_action");
                intent.addCategory("time_line_category");
                //bd = new Bundle();
                //bd.putBoolean("IsMulEnable", mIsMulEnable);
                //intent.putExtras(bd);
                startActivity(intent);
                break;
        }
    }

    private void showWarningDialog(String strMsg){

        final AlertDialog.Builder normalDialog = new AlertDialog.Builder(this);
        //normalDialog.setIcon(R.drawable.icon_dialog);
        normalDialog.setTitle(getString(R.string.warning));
        normalDialog.setMessage(strMsg);
        normalDialog.setPositiveButton(getString(R.string.result_ok),
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // nothing to do currently
                    }
                });
        // 显示
        normalDialog.show();
    }



    private void getSharedPref() {
        SharedPreferences pref = getSharedPreferences("main_config", MODE_PRIVATE);

        mTotalQuality = pref.getInt("totalQuality", 50);
        mMaxValue = pref.getInt("maxValue", 100);
        mIsAddEnable = pref.getBoolean("isAddEnable", true);
        mIsSubEnable = pref.getBoolean("isSubEnable", true);
        mIsMulEnable = pref.getBoolean("isMulEnable", false);
        mIsDivEnable = pref.getBoolean("isDivEnable", false);
    }

    private void setSharedPref() {
        SharedPreferences pref = getSharedPreferences("main_config", MODE_PRIVATE);
        SharedPreferences.Editor editor = pref.edit();

        editor.putInt("totalQuality", mTotalQuality);
        editor.putInt("maxValue", mMaxValue);
        editor.putBoolean("isAddEnable", mIsAddEnable);
        editor.putBoolean("isSubEnable", mIsSubEnable);
        editor.putBoolean("isMulEnable", mIsMulEnable);
        editor.putBoolean("isDivEnable", mIsDivEnable);
        editor.commit();
    }
}
